package com.dataseek.iot.sys.manager.controller;


import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dataseek.iot.core.base.IController;
import com.dataseek.iot.core.constant.ResponseConst;
import com.dataseek.iot.core.request.QueryPage;
import com.dataseek.iot.core.request.QueryPage2;
import com.dataseek.iot.core.request.QueryParam;
import com.dataseek.iot.core.response.ResponseEntity;
import com.dataseek.iot.core.util.ParseUtil;
import com.dataseek.iot.core.util.WrapperUtil;
import com.dataseek.iot.mysql.entity.Area;
import com.dataseek.iot.sys.manager.handler.AreaServiceHandler;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

import java.util.List;

/**
 * <p>
 * 行政区域县区信息表 前端控制器
 * </p>
 *
 * @author cc
 * @since 2021-10-26
 */
@RestController
@RequestMapping("/area")
@Api(tags = "区县信息接口")
public class AreaController implements IController<Area> {
    
    @Autowired
    AreaServiceHandler areaService;


    @PutMapping("/addOne")
    @ApiOperation("保存单个区县")
    @Override
    public Mono<ResponseEntity> saveOne(@RequestBody Area entity) {
        return areaService.saveOne(entity);
    }

    @PostMapping("/addBatch")
    @ApiOperation("批量保存区县数据")
    @Override
    public Mono<ResponseEntity> saveBatch(@RequestBody List<Area> lists) {
        return areaService.saveBatch(lists);
    }


    @DeleteMapping("/{id}")
    @ApiOperation("根据ID移除区县信息")
    @Override
    public Mono<ResponseEntity> remove(@PathVariable String id) {
        return areaService.remove(id);
    }

    @DeleteMapping("/removeByIds")
    @ApiOperation("根据ID批量移除区县信息")
    @Override
    public Mono<ResponseEntity> removeAll(@RequestBody List<String> ids) {
        return areaService.removeAll(ids);
    }

    @DeleteMapping("/remove")
    @ApiOperation("根据条件移除区县信息")
    @Override
    public Mono<ResponseEntity> remove(@RequestBody List<QueryParam> params) {
        Wrapper wrapper = WrapperUtil.assembleParams(params);
        return areaService.remove(wrapper);
    }

    @PostMapping("/updateOne")
    @ApiOperation("更新区县信息")
    @Override
    public Mono<ResponseEntity> updateOne(@RequestBody Area entity) {
        return areaService.updateOne(entity);
    }

    @PostMapping("/update")
    @ApiOperation("根据条件更新区县信息")
    @Override
    public Mono<ResponseEntity> update(@RequestBody List<QueryParam> params) {
        Wrapper wrapper = WrapperUtil.assembleParams(params);
        return areaService.update(wrapper);
    }

    @PostMapping("/updateBatch")
    @ApiOperation("批量更新")
    @Override
    public Mono<ResponseEntity> updateBatch(@RequestBody List<Area> entities) {
        return areaService.updateBatch(entities);
    }

    @PostMapping("/page/find")
    @ApiOperation("分页查询")
    @Override
    public Mono<ResponseEntity> page(@RequestBody Page p) {
        return areaService.page(p);
    }

    @PostMapping("/page/findWithParam")
    @ApiOperation("分页查询，带查询条件")
    @Override
    public Mono<ResponseEntity> page(@RequestBody QueryPage queryPage) {
        if(queryPage==null||queryPage.getParams()==null||queryPage.getPage()==null){
            ResponseEntity entity  = new ResponseEntity();
            entity.setCode(ResponseConst.BAD_REQUEST_CODE);
            entity.setMsg(ResponseConst.BAD_REQUEST);
            return areaService.defaultResponseOne(entity);
        }
        return areaService.page(queryPage.getPage(), WrapperUtil.assembleParams(queryPage.getParams()));
    }

    /**
     * 根据IDS查询数据
     * @param ids
     * @return
     */
    @PostMapping("/findById")
    @ApiOperation("根据ID查询数据")
    @Override
    public Flux<ResponseEntity> findByIds(@RequestBody List<String> ids) {
        return areaService.find(ids);
    }

    /**
     * 根据查询条件查询数据
     * @param params
     * @return
     */
    @PostMapping("/findByParams")
    @ApiOperation("根据查询条件查询参数")
    @Override
    public Flux<ResponseEntity> find(@RequestBody List<QueryParam> params){
        ResponseEntity entity = new ResponseEntity();
        if(params==null){
            entity.setCode(ResponseConst.BAD_REQUEST_CODE);
            entity.setMsg(ResponseConst.BAD_REQUEST);
            return areaService.defaultResponseTwo(entity);
        }else if(params.size()==0){
            entity.setCode(ResponseConst.NULL_PARAM_CODE);
            entity.setMsg(ResponseConst.NULL_PARAM);
            return areaService.defaultResponseTwo(entity);
        }
        return areaService.find(WrapperUtil.assembleParams(params));
    }

    /**
     * 根据id条件查询数据
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation("根据ID查询区县")
    @Override
    public Mono<ResponseEntity> findOne(@PathVariable String id) {
        return areaService.findOne(id);
    }


    /**
     * 根据id条件查询数据
     * @param code
     * @return
     */
    @GetMapping("/{code}")
    @ApiOperation("根据code查询区县")
    public Mono<ResponseEntity> findOneByCode(@PathVariable String code){
        return areaService.findOneByCode(code);
    }

    @GetMapping("/parentCode/{parentCode}")
    @ApiOperation("根据城市编码查询下辖区县")
    public Flux<ResponseEntity> findAllByParentCode(@PathVariable String parentCode){
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("city_id", parentCode);
        return areaService.find(wrapper);
    }
    /**
     * 查询所有数据
     * @return
     */
    @PostMapping("/findAll")
    @ApiOperation("查询所有")
    @Override
    public Flux<ResponseEntity> findAll() {
        return areaService.findAll();
    }


    @PostMapping("/findByJSONParams")
    @ApiOperation("根据json对象为查询条件查询")
    @Override
    public Flux<ResponseEntity> find(@RequestBody JSONObject json) {
        QueryWrapper wrapper = ParseUtil.parseJson(json);
        return areaService.find(wrapper);
    }

    @PostMapping("/page/findByJSONParams")
    @ApiOperation("分页查询，传参为json")
    @Override
    public Mono<ResponseEntity> page(@RequestBody JSONObject json) {
        QueryPage2 queryPage2 = ParseUtil.parseJsonWithPage(json);
        return areaService.page(queryPage2.getPage(),queryPage2.getWrapper());
    }

}
